package cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.tagdictionary.TagDictionaryDO;
import cn.gemiman.daoyi.module.cms.service.tagdictionary.TagDictionaryService;

@Tag(name = "管理后台 - 文章标签")
@RestController
@RequestMapping("/cms/tag-dictionary")
@Validated
public class TagDictionaryController {

    @Resource
    private TagDictionaryService tagDictionaryService;

    @PostMapping("/create")
    @Operation(summary = "创建文章标签")
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:create')")
    public CommonResult<Long> createTagDictionary(@Valid @RequestBody TagDictionarySaveReqVO createReqVO) {
        return success(tagDictionaryService.createTagDictionary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章标签")
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:update')")
    public CommonResult<Boolean> updateTagDictionary(@Valid @RequestBody TagDictionarySaveReqVO updateReqVO) {
        tagDictionaryService.updateTagDictionary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:delete')")
    public CommonResult<Boolean> deleteTagDictionary(@RequestParam("id") Long id) {
        tagDictionaryService.deleteTagDictionary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:query')")
    public CommonResult<TagDictionaryRespVO> getTagDictionary(@RequestParam("id") Long id) {
        TagDictionaryDO tagDictionary = tagDictionaryService.getTagDictionary(id);
        return success(BeanUtils.toBean(tagDictionary, TagDictionaryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章标签分页")
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:query')")
    public CommonResult<PageResult<TagDictionaryRespVO>> getTagDictionaryPage(@Valid TagDictionaryPageReqVO pageReqVO) {
        PageResult<TagDictionaryDO> pageResult = tagDictionaryService.getTagDictionaryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TagDictionaryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章标签 Excel")
    @PreAuthorize("@ss.hasPermission('cms:tag-dictionary:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagDictionaryExcel(@Valid TagDictionaryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TagDictionaryDO> list = tagDictionaryService.getTagDictionaryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章标签.xls", "数据", TagDictionaryRespVO.class,
                        BeanUtils.toBean(list, TagDictionaryRespVO.class));
    }

}